﻿using System.Windows.Controls;
using System.Windows.Input;

namespace FrontDisplay.Weather
{
    /// <summary>
    /// Interaction logic for WeatherPage.xaml
    /// </summary>
    public partial class WeatherPage : UserControl
    {
        private WeatherInfo _weather;

        public WeatherPage()
        {
            InitializeComponent();

            MainPanel.ScrollPanel.PreviewMouseLeftButtonDown += PageMouseDown;

            IsVisibleChanged += WeatherPageIsVisibleChanged;
        }

        private void WeatherPageIsVisibleChanged(object sender, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                UpdateWeather();
                MainPanel.ScrollToLeftEnd();
            }
        }

        public void UpdateWeather(WeatherInfo weather)
        {
            _weather = weather;
            UpdateWeather();
        }

        internal void UpdateWeather()
        {
            MainPanel.Clear();

            if (_weather == null)
            {
                CityName.Text = string.Empty;
                return;
            }
            CityName.Text = _weather.City;
            foreach (var weatherItem in _weather.Forecast)
            {
                WeatherItemControl weatherItemControl = new WeatherItemControl();
                weatherItemControl.Update(weatherItem);
                MainPanel.Add(weatherItemControl);
                ThemeManager.UpdateControlForeground(weatherItemControl);
            }
        }

        private void PageMouseDown(object sender, MouseButtonEventArgs e)
        {
            this.NavigateBack();
        }
    }
}
